<template>
  <van-address-edit
    :area-list="areaList"
    :address-info="addresInfo"
    show-postal
    show-delete
    show-set-default
    show-search-result
    :area-columns-placeholder="['请选择', '请选择', '请选择']"
    @save="handleSave"
    @delete="handleDel"
  />
</template>

<script setup lang="ts">
import { areaList } from "@vant/area-data";
import { AddressType } from "../../types/user";
import { useAddress, useUserStore } from "../../store/user";
import { useRoute } from "vue-router";
import { ref } from "vue";
const route = useRoute();
const address = useAddress();
const user = useUserStore();
const addresInfo = ref<AddressType>();
let idx = Number(route.params.idx);
console.log(idx);

if (idx) {
  addresInfo.value = address.list[idx];
}

const handleSave = (content: AddressType) => {
  // if(content.createdAt){
  //   delete content.createdAt;
  // }
  console.log("保存", content);
  let userid = user.userInfo?.objectId;
  //新增
  if (userid && idx == -1) {
    address.addressAddAction({ ...content, userid });
    return;
  }
  // 编辑
  address.addressUpdateAction(address.list[idx].objectId, content);
};
const handleDel = () => {
  console.log("删除");
};
</script>

<style scoped></style>
